package edu.dublbo.app.generator.entity;

import edu.dublbo.app.generator.util.StrUtils;
import lombok.Getter;
import lombok.Setter;
import lombok.ToString;

import java.io.Serializable;
import java.util.Date;
import java.util.List;

/**
 * @author habitplus
 * @since 2023/3/12 21:33
 */
@Getter
@Setter
@ToString
public class TProjectInfo implements Serializable {
    private String id;
    private String projectName;
    private String projectDesc;
    private String packageName;
    private String createUser;
    private Date createTime;
//    private String modifyUser;
//    private Date modifyTime;
    private Integer deleteFlag;
    private List<ModuleInfo> moduleList;

    public static boolean checkValid(TProjectInfo o) {
        return o != null
                && !StrUtils.isBlank(o.getProjectName())
                && o.checkPackageDir()
                && o.getModuleList() != null
                && o.getModuleList().size() > 0;
    }

    private boolean checkPackageDir() {
        if (StrUtils.isEmpty(this.packageName)) {
            return true;
        }

        String[] ss = this.packageName.split("\\.");

        // 最多三级
        if (ss.length > 3) {
            return false;
        }

        StringBuilder sb = new StringBuilder();
        // 每一级必须是合法的类名
        for (String s : ss) {
            if (!StrUtils.isValidClassName(s)) return false;
            sb.append(s).append(".");
        }

        this.packageName = sb.substring(0, sb.length() - 1);

        return true;
    }

    public static class ModuleInfo {
        private String name;
        private List<String> modelIds;

        public String getName() {
            return name;
        }

        public void setName(String name) {
            this.name = name;
        }

        public List<String> getModelIds() {
            return modelIds;
        }

        public void setModelIds(List<String> modelIds) {
            this.modelIds = modelIds;
        }

        @Override
        public String toString() {
            return "ModuleInfo{" +
                    "name='" + name + '\'' +
                    ", modelIds=" + modelIds +
                    '}';
        }
    }
}
